﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CUWo.Common.Entities;
using CUWo.Common.StorageHelper;

/**
 * 创建时间：2014/11/21 15:20:57
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace CUWo.Common
{
    public class OperationStatus
    {
        public bool Status { get; set; }

        public string Message { get; set; }
    }

    public class RomHelper
    {

        /// <summary>
        /// 获取Rom
        /// </summary>
        /// <returns></returns>
        public static RomInfo GetRomInfo()
        {
            if (!IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                return null;
            }
            string json = IsolatedStorageHelper.Instance.ReadText(Keys.ROM_FILENAME);
            if (string.IsNullOrWhiteSpace(json))
            {
                return null;
            }
            else
            {
                return JSONHelper.Deserialize<RomInfo>(json);
            }
        }
        public static OperationStatus UpdateRom(string mobile, string password)
        {
            OperationStatus result = new OperationStatus() { Status = false };
            //if(string.IsNullOrWhiteSpace(mobile) || string.IsNullOrWhiteSpace(password))
            if (string.IsNullOrWhiteSpace(mobile))
            {
                result.Message = "手机号不能空！";
                return result;
            }
            if (!IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                RomInfo romInfo = new RomInfo()
                {
                    Mobile = mobile,
                    Password = password,
                    IsPush = true
                };
                IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
            }
            else
            {
                string json = IsolatedStorageHelper.Instance.ReadText(Keys.ROM_FILENAME);
                if (string.IsNullOrWhiteSpace(json))
                {
                    RomInfo romInfo = new RomInfo()
                    {
                        Mobile = mobile,
                        Password = password,
                        IsPush = true
                    };
                    IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
                }
                else
                {
                    RomInfo item = JSONHelper.Deserialize<RomInfo>(json);
                    if (item == null)
                    {
                        RomInfo romInfo = new RomInfo()
                        {
                            Mobile = mobile,
                            Password = password,
                            IsPush = true
                        };
                        IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
                    }
                    else
                    {
                        item.Mobile = mobile;
                        item.Password = password;
                        IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(item), Keys.ROM_FILENAME, true);
                    }
                }
            }
            result.Status = true;
            result.Message = "更新成功！";
            return result;
        }

        public static OperationStatus UpdateRom(RomInfo romInfo)
        {
            OperationStatus result = new OperationStatus() { Status = false };

            if (romInfo == null)
            {
                result.Message = "数据源为空，无法保存！";
                return result;
            }
            if (romInfo.IsEmpty())
            {
                result.Message = "数据源为空，无法保存！";
                return result;
            }

            if (!IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
            }
            else
            {
                string json = IsolatedStorageHelper.Instance.ReadText(Keys.ROM_FILENAME);
                if (string.IsNullOrWhiteSpace(json))
                {
                    IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
                }
                else
                {
                    RomInfo item = JSONHelper.Deserialize<RomInfo>(json);
                    if (item == null)
                    {
                        IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
                    }
                    else
                    {
                        item.Mobile = romInfo.Mobile;
                        item.Password = romInfo.Password;
                        item.IsPush = romInfo.IsPush;
                        IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(item), Keys.ROM_FILENAME, true);
                    }
                }
            }
            result.Status = true;
            result.Message = "更新成功！";
            return result;
        }
    }
}
